<?php
/**
 * MiniFeed Service
 *
 * @package  Addons
 * @author   Wing <wxywing@gmail.com>
 * @version  $Id: MiniFeedService.class.php Wed Apr 25 09:14:47 GMT 2012 wing $ 
 */
class MiniFeedService extends Service {
  
	public function __construct() {}
	
	/**
	 * 发布动态
	 *
	 * @param array  $data 动态的数据数组
	 * @param string $type 动态类型
	 * @return boolean
	 */
	public function put($data, $type) 
	{
		return model('MiniFeed')->feed($data, $type);
	}
	
	public function getFeedByMap($map, $limit = 10, $order = 'id DESC')
	 {
		$list = model('MiniFeed')->where($map)->order($order)->findPage($limit);
		foreach ($list['data'] as $key => $value) 
		{
			$list['data'][$key] = $this->_parseData($value);
		}

		return $list;
	}
	
	private function _parseData($data)
	{
		$params = unserialize($data['params']);
		$type   = $data['type'];
		
		$type_arr = explode ( '_',  $type);
		$entry   = ucfirst($type_arr[0]);

		$return = array();
		foreach ($params as $key => $value)
		{
			$map[$key] = $value;
			$detail =  M($entry)->where($map)->find();
			$content = $detail['content'];
		}
		
		$replace ["{actor}"] = '<a href="' . U("home/space/index", array("uid" => $data['feed_uid'])) . '" target="_blank">' . getUserName($data ['feed_uid']) . '</a>';
		$template = require (ADDON_PATH . '/widgets/feed/language/cn/feed.php');
		
		$return ['detail'] = $detail;
		$return ['title'] = str_replace(array_keys($replace), array_values($replace), $template[$type]['title']);
		$return ['body']  = str_replace(array_keys($replace), array_values($replace), $template[$type]['body']);
		return $return;
	}
	
	public function run(){}
}